package ysq.xyg.task;/**
 * Created by Administrator on 2018/7/24.
 */

import com.github.pagehelper.StringUtil;
import net.sf.json.JSONObject;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import ysq.weixin.client.WxApiClient;
import ysq.weixin.client.WxMemoryCacheClient;
import ysq.weixin.model.TemplateMessage;
import ysq.xyg.model.Product;
import ysq.xyg.model.info.WxAccountInfo;
import ysq.xyg.service.ProductService;
import ysq.xyg.util.BaseConstant;
import ysq.xyg.util.DateUtil;
import ysq.xyg.util.ProProperties;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 发送定时通知
 *
 * @author ysq
 * @create 2018-07-24 17:13
 **/
@Component
public class SendNoticeTask {
    @Resource
    private ProductService productService;

    //@Scheduled(cron = "* * 0/1 * * ? ")// @Scheduled(cron = "0 0 * * * ?") // 间隔1小时执行

    @Scheduled(cron = "0 0 * * * ?") // 间隔1小时执行
    public void taskCycle() {
        sendTask("");
    }

    public void sendTask(String openid){
        List<Product> deviceList=productService.listLeft(new Product(BaseConstant.PRODUCT_TYPE_JCY,openid));
        if(deviceList!=null &&deviceList.size()>0){
            for(Product product:deviceList){
                Product productData=null;
                try {
                    productData=productService.selectData(new Product(product.getDeviceId()));
                }catch (Exception e){
                    continue;
                }
                if(productData!=null && product.getMaxSendValue()!=null) {
                    if ("1".equals(product.getType()) || "2".equals(product.getType()) || "3".equals(product.getType())
                            || "4".equals(product.getType()) || "5".equals(product.getType()) || "6".equals(product.getType())
                            || "14".equals(product.getType()) || "15".equals(product.getType()) || "16".equals(product.getType())) {
                        //AQI
                        if (StringUtil.isNotEmpty(productData.getAqi())) {
                            int aqi = Integer.parseInt(productData.getAqi());
                            if (aqi >= product.getMaxSendValue() && isSendNote(product)) {
                                send("您的设备AQI为" + productData.getAqi() + ",超过预警值!", product.getName(), product.getOpenid(), product.getId(),product.getWxId());
                            }
                        }
                    } else if ("7".equals(product.getType()) || "8".equals(product.getType())) {
                        //CO2
                        if (StringUtil.isNotEmpty(productData.getCo2())) {
                            int co2 = Integer.parseInt(productData.getCo2());
                            if (co2 >= product.getMaxSendValue() && isSendNote(product)) {
                                send("您的设备CO2为" + productData.getCo2() + ",超过预警值!", product.getName(), product.getOpenid(), product.getId(),product.getWxId());
                            }
                        }
                    } else if ("9".equals(product.getType()) || "10".equals(product.getType())) {
                        //O2
                        if (StringUtil.isNotEmpty(productData.getO2())) {
                            Double o2 = Double.parseDouble(productData.getO2());
                            if (o2 >= product.getMaxSendValue() && isSendNote(product)) {
                                send("您的设备O2为" + productData.getO2() + ",超过预警值!", product.getName(), product.getOpenid(), product.getId(),product.getWxId());
                            }
                        }
                    } else if ("11".equals(product.getType()) || "12".equals(product.getType()) || "13".equals(product.getType())) {
                        //hcho
                        if (StringUtil.isNotEmpty(productData.getHcho())) {
                            Double hcho = Double.parseDouble(productData.getHcho());
                            if (hcho >= product.getMaxSendValue() && isSendNote(product)) {
                                send("您的设备HCHO为" + productData.getHcho() + ",超过预警值!", product.getName(), product.getOpenid(), product.getId(),product.getWxId());
                            }
                        }
                    } else if ("17".equals(product.getType())) {
                        //temp
                        if (StringUtil.isNotEmpty(product.getTemr())) {
                            int temr = Integer.parseInt(product.getTemr());
                            if (temr >= product.getMaxSendValue() && isSendNote(product)) {
                                send("您的设备TEMP为" + productData.getTemr() + ",超过预警值!", product.getName(), product.getOpenid(), product.getId(),product.getWxId());
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * 是否发送通知，如果没有发送过或者发送时间超过1天可以发送消息
     * @param product
     * @return
     */
    public boolean isSendNote(Product product){
        /*if(product.getLastSendTime()==null||DateUtil.getDateOfDate(product.getLastSendTime(),-1).getTime()<new Date().getTime()){
            product.setLastSendTime(new Date());
            productService.updateByPrimaryKey(product);
            return true;
        }*/
        return true;
    }

    /**
     * 发送微信端预警通知接口
     */
    public void send(String title,String device,String openid,String productId,String wxId){
        TemplateMessage templateMessage=new TemplateMessage();
        WxAccountInfo wxAccountInfo = WxMemoryCacheClient.getSingleWxAccountInfo(wxId);//获取缓存中的唯一账号
        if(wxAccountInfo!=null&&StringUtil.isNotEmpty(wxAccountInfo.getTemplateId())){
            String wxHomeWorkURLURL=ProProperties.getInstance().getWxProductDescURL(openid,productId,wxId);
            templateMessage.setUrl(wxAccountInfo.getBaseURL()+wxHomeWorkURLURL);
            templateMessage.setOpenid(openid);
            templateMessage.setTemplateId(wxAccountInfo.getTemplateId());
            Map<String,String> data=new HashMap<String, String>();
            data.put("first",title);
            data.put("device",device);
            data.put("time",DateUtil.formatFromDateHm(new Date()));
            data.put("remark","备注：请尽快查看更详细的信息");
            templateMessage.setDataMap(data);
            JSONObject jsonObject= WxApiClient.sendTempletNotice(templateMessage,wxId);

        }

    }
}
